package com.company.basic;

import java.util.*;

public class SumOfThree {

    public static class Three{
        int a;
        int b;
        int c;
        int r;

        public Three(int r) {
            this.r = r;
        }
    }

    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();

        Set<Integer> set=new HashSet<>();
        for (int c : nums) {
            if (set.contains(c)) {
                continue;
            }
            set.add(c);
            List<List<Integer>> lists = twoSum(nums, -c);
            for (List<Integer> list : lists) {
                list.add(c);
            }
            result.addAll(lists);
        }
        return result;
    }



    public List<List<Integer>> twoSum(int[] nums, int result) {
       Map<Integer,Integer> targetMap=new HashMap<>();
        List<List<Integer>> resultLists = new ArrayList<>();
        for (int a : nums) {
            int b = result - a;
            if (targetMap.containsKey(b)) {
                continue;
            }
            targetMap.put(b,a);


            if (targetMap.get(b) != null) {
                List<Integer> list = new ArrayList<>();
                list.add(b);
                list.add(targetMap.get(b));
                resultLists.add(list);
            }
        }
        return resultLists;
    }


}
